﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media;

namespace FrankensteamClient
{
    class TabHandler : TabItem
    {
        PrivateChatContent _content;

        // Constructor
    public TabHandler()
    {
        // Create an instance of the usercontrol
        TabHeader tabHeader = new TabHeader();

        // Assign the usercontrol to the tab header
        this.Header = tabHeader;

        // Attach to the TabHeader events (Mouse Enter/Leave, Button Click, and Label resize)
        tabHeader.btnCloseTab.MouseEnter += new MouseEventHandler(button_close_MouseEnter);
        tabHeader.btnCloseTab.MouseLeave += new MouseEventHandler(button_close_MouseLeave);
        tabHeader.btnCloseTab.Click += new RoutedEventHandler(button_close_Click);
        tabHeader.lblTabTitle.SizeChanged += new SizeChangedEventHandler(label_TabTitle_SizeChanged);

        this.Content = _content;
    }


    /// <summary>
    /// Property - Set the Title of the Tab
    /// </summary>
    public string Title
    {
        set
        {
            ((TabHeader)this.Header).lblTabTitle.Content = value;
        }
        get
        {
            return ((TabHeader)this.Header).lblTabTitle.Content.ToString();
        }
    }

    public PrivateChatContent ContentInstance
    {
        get
        {
            if (_content == null)
            {
                _content = new PrivateChatContent();
            }

            return _content;
        }
        set
        {
            _content = value;
            this.Content = _content;
        }
    }

    //
    // - - - Overrides  - - -
    //


    // Override OnSelected - Show the Close Button
    protected override void OnSelected(RoutedEventArgs e)
    {
        base.OnSelected(e);
        ((TabHeader)this.Header).btnCloseTab.Visibility = Visibility.Visible;
    }

    // Override OnUnSelected - Hide the Close Button
    protected override void OnUnselected(RoutedEventArgs e)
    {
        base.OnUnselected(e);
        ((TabHeader)this.Header).btnCloseTab.Visibility = Visibility.Hidden;
    }

    // Override OnMouseEnter - Show the Close Button
    protected override void OnMouseEnter(MouseEventArgs e)
    {
        base.OnMouseEnter(e);
        ((TabHeader)this.Header).btnCloseTab.Visibility = Visibility.Visible;
    }

    // Override OnMouseLeave - Hide the Close Button (If it is NOT selected)
    protected override void OnMouseLeave(MouseEventArgs e)
    {
        base.OnMouseLeave(e);
        if (!this.IsSelected)
        {
            ((TabHeader)this.Header).btnCloseTab.Visibility = Visibility.Hidden;
        }
    }


    //
    // - - - Event Handlers  - - -
    //


    // Button MouseEnter - When the mouse is over the close button - change color to white
    void button_close_MouseEnter(object sender, MouseEventArgs e)
    {
        ((TabHeader)this.Header).btnCloseTab.Foreground = Brushes.White;
    }

    // Button MouseLeave - When mouse is no longer over close button - change color to gray 
    void button_close_MouseLeave(object sender, MouseEventArgs e)
    {
        ((TabHeader)this.Header).btnCloseTab.Foreground = Brushes.Gray;
    }


    // Button Close Click - Remove the Tab - (or raise an event indicating a "CloseTab" event has occurred)
    void button_close_Click(object sender, RoutedEventArgs e)
    {
        ((TabControl)this.Parent).Items.Remove(this);
    }


    // Label SizeChanged - When the Size of the Label changes (due to setting the Title) set position of button properly
    void label_TabTitle_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        ((TabHeader)this.Header).btnCloseTab.Margin = new Thickness(((TabHeader)this.Header).lblTabTitle.ActualWidth + 10, 3, 4, 0);
    }
    }
}
